package com.rui.study.micro.goods.mapper;

import com.rui.study.micro.goods.model.GoodsPo;
import org.apache.ibatis.annotations.*;

@Mapper
public interface GoodsMapper {

    /**
     * 新增一款商品
     * @param goodsPo
     * @return
     */
    @Insert("INSERT INTO g_goods_define (" +
            "   name," +
            "   category_id," +
            "   intro," +
            "   price," +
            "   picture_url," +
            "   status," +
            "   state," +
            "   merchant_id," +
            "   stock," +
//            "   delete," +
            "   create_time," +
            "   update_time)" +
            "VALUES (" +
            "   #{name}," +
            "   #{categoryId}," +
            "   #{intro}," +
            "   #{price}," +
            "   #{pictureUrl}," +
            "   #{status}," +
            "   #{state}," +
            "   #{merchantId}," +
            "   #{stock}," +
//            "   #{delete}," +
            "   NOW()," +
            "   #{updateTime})")
    int insertUserInfo(GoodsPo goodsPo);


    /**
     * 更新商品存量，指定商品id
     * @param goodsId 商品id
     * @param count 增加的数量，负数表示出库，正数表示入库
     * @return
     */
    @Update("UPDATE " +
            "   g_goods_define " +
            "SET " +
            "   stock = stock + (#{count}) " +
            "WHERE " +
            "   goods_id = #{goods_id} " +
            "AND " +
            "   (stock + (#{count})) >= 0")
    int updateGoodsStock(@Param("goods_id") String goodsId, @Param("count")long count);


    /**
     * 查询商品存量，指定商品id
     * @param goodsId 商品id
     * @return
     */
    @Select("SELECT " +
            "   stock " +
            "FROM " +
            "   g_goods_define " +
            "WHERE " +
            "   goods_id = #{goodsId}")
    long queryGoodsStock(@Param("goodsId") String goodsId);
}
